/**
 * 后期处理下数据
 */
var config = {
    url: 'http://www.1hcz9.com/',
    tryUrl: 'http://try.1hcz9.com/',
    adminUrl: 'http://admin.1hcz9.com/',
};

var fs = require("fs");
var path = require("path");

function mkdir(dir) {
    var arr = dir.split("/");
    var p = "";
    for (var i in arr) {
        p += arr[i] + "/";
        if (!fs.existsSync(p)) fs.mkdirSync(p);
    }
}

function copy(src, dst) {
    if (!fs.existsSync(src)) {
        console.log("源文件不存在", src);
        return;
    }
    var dir = path.dirname(dst);
    if (!fs.existsSync(dir)) mkdir(dir);
    fs.writeFileSync(dst, fs.readFileSync(src));
}

function visit(dir, cb) {
    if (!fs.existsSync(dir)) {
        console.log("源文件不存在", dir);
        return;
    }
    fs.readdirSync(dir).forEach(function(file) {
        var p = dir + "/" + file;
        if (fs.statSync(p).isDirectory()) {
            visit(p, cb);
        }
        else {
            cb(p);
        }
    });
}

function copyDir(src, dst) {
    visit(src, function(p) {
        copy(p, dst + p.replace(src, ""));
    });
}

function deleteDir(dir) {
    var files = [];
    if (fs.existsSync(dir)) {
        files = fs.readdirSync(dir);
        files.forEach(function(file, index) {
            var currPath = dir + "/" + file;
            if (fs.statSync(currPath).isDirectory()) deleteDir(currPath);
            else fs.unlinkSync(currPath);
        });
        fs.rmdirSync(dir);
    }
}

function replaceFile(files, data) {
    for (var i in files) {
        var file = files[i];
        var content = fs.readFileSync(file).toString();
        for (var k in data) {
            content = content.replace(k, data[k]);
        }
        fs.writeFileSync(file, content);
    }
}

function copyFullDir(dst, url, tryUrl, extra) {
    copyDir("build/admin/", "build/" + dst + "/admin/");
    copyDir("build/admin/", "build/" + dst + "/admin_try/");
    copyDir("build/agm/", "build/" + dst + "/agm/");
    copyDir("build/agm/", "build/" + dst + "/agm_try/");
    copyDir("build/ag/", "build/" + dst + "/ag/");
    copyDir("build/ag/", "build/" + dst + "/ag_try");
    copyDir("build/js/", "build/" + dst + "/js/");
    copyDir("build/public/", "build/" + dst + "/public/");
    copyDir("build/js/", "build/" + dst + "/js/");
    
    copyDir("build/referee/", "build/" + dst + "/referee");
    copyDir("build/referee/", "build/" + dst + "/referee_try");
    copy("build/index.html", "build/" + dst + "/index.html");

    // 修改下试玩后台参数
    replaceFile(["build/" + dst + "/admin_try/login.html"], {
        "<title>后台管理 - 登录</title>": "<title>试玩后台管理 - 登录</title>",
        "欢迎进入后台管理系统！": "欢迎进入试玩后台管理系统！"
    });
    replaceFile(["build/" + dst + "/admin_try/admin.html"], {
        "<title>后台管理 - 登录</title>": "<title>试玩后台管理 - 登录</title>",
        "欢迎进入后台管理系统！": "欢迎进入试玩后台管理系统！",
        "var _tryOnly = false;": "var _tryOnly = true;",
    });
    replaceFile([ "build/" + dst + "/admin_try/pages/index.html",
        "build/" + dst + "/admin/pages/index.html"], {
        "{{TRY_FRONT}}": tryUrl + "index.html",
        "{{FRONT}}": url + extra + "/index.html",
    });

    replaceFile([ "build/" + dst + "/admin_try/js/admin.min.js", 
        "build/" + dst + "/admin_try/js/login.min.js",
        "build/" + dst + "/ag_try/js/center.min.js", 
        "build/" + dst + "/ag_try/js/login.min.js", 
        "build/" + dst + "/ag_try/js/main2.min.js",
        "build/" + dst + "/" + extra + "_try/js/center.min.js", 
        "build/" + dst + "/" + extra + "_try/js/login.min.js", 
        "build/" + dst + "/" + extra + "_try/js/main2.min.js",
        "build/" + dst + "/" + extra + "_try/login/js/" + extra + ".min.js",
        "build/" + dst + "/agm_try/js/center.min.js", 
        "build/" + dst + "/agm_try/js/index.min.js", 
        "build/" + dst + "/agm_try/js/main2.min.js",
        "build/" + dst + "/agm_try/js/login.min.js",
        "build/" + dst + "/referee_try/js/referee.min.js",
    ],
    {
        // 替换试玩的网址
        "http://www.1hc8.com/" : tryUrl,
        "http://www.1hc8.com:9090/": tryUrl,
    });

    replaceFile([ "build/" + dst + "/agm/js/main2.min.js", 
        "build/" + dst + "/agm/js/login.min.js",
        "build/" + dst + "/agm/js/center.min.js", 
        "build/" + dst + "/agm/js/index.min.js", 
        "build/" + dst + "/public/all/js/context.js", 
        "build/" + dst + "/admin/js/admin.min.js",
        "build/" + dst + "/admin/js/login.min.js",
        //"build/" + dst + "/js/public.min.js",
        "build/" + dst + "/" + extra + "/login/js/" + extra + ".min.js",
        "build/" + dst + "/" + extra + "/js/center.min.js",
        "build/" + dst + "/" + extra + "/js/login.min.js",
        "build/" + dst + "/" + extra + "/js/main2.min.js",
        "build/" + dst + "/referee/js/referee.min.js",
    ], 
    {
        "http://www.1hc8.com/" : url,
        "http://www.1hc8.com:9090/" : tryUrl,
    });
}

// 趣购彩(前端)
copyDir("build/ag/", "build/qgc2/");
copyDir("build/qgc/", "build/qgc2/");

// 测试服，直接copy就可以了
console.log("* 测试服...");
copyDir("build/qgc2/", "build/test/qgc/");
copyDir("build/qgc2/", "build/test/qgc_try/");
copyFullDir("test", "http://www.1hc8.com/", "http://www.1hc8.com:9090/", "qgc");

// 趣购彩
console.log("趣购彩...");
copyDir("build/qgc2/", "build/www.qgc600.com/qgc/");
copyDir("build/qgc2/", "build/www.qgc600.com/qgc_try/");
copyFullDir("www.qgc600.com", "http://www.qgc600.com/", "http://try.qgc600.com/", "qgc");
deleteDir("build/www.qgc600.com/ag");
deleteDir("build/www.qgc600.com/ag_try");
deleteDir("build/qgc2");

// 爱尚彩
console.log("爱尚彩...");
copyDir("build/ag/", "build/asc/");
copyDir("build/asc99/", "build/asc/");
copyDir("build/asc/", "build/www.asc99.com/asc/");
copyDir("build/asc/", "build/www.asc99.com/asc_try/");
copyFullDir("www.asc99.com", "http://www.asc99.com/", "http://try.asc99.com/", "asc");
copyDir("build/asc/agm", "build/www.asc99.com/agm/");
copyDir("build/asc/agm", "build/www.asc99.com/agm_try/");
deleteDir("build/www.asc99.com/ag");
deleteDir("build/www.asc99.com/ag_try");
deleteDir("build/asc");
replaceFile(["build/www.asc99.com/asc/main2.html", "build/www.asc99.com/asc_try/main2.html"], {
    '<li class="r-wrap r-nowrap1" id="kjzb_div">': '<li class="r-wrap r-nowrap1 hide" id="kjzb_div">',
    '<li class="r-wrap r-nowrap1" id="kjzb2_div">': '<li class="r-wrap r-nowrap1 hide" id="kjzb2_div">',
});
replaceFile(["build/www.asc99.com/agm/index.html"], {
    '../qgc/index.html': '../asc/index.html',
});
replaceFile(["build/www.asc99.com/agm_try/index.html"], {
    '../qgc/index.html': '../asc_try/index.html',
});

// UG彩
console.log("UG彩...");
copyDir("build/ag/", "build/ug92/");
copyDir("build/ug9/", "build/ug92/");
copyDir("build/ug92/", "build/www.ugc99.com/ug9/");
copyDir("build/ug92/", "build/www.ugc99.com/ug9_try/");
copyFullDir("www.ugc99.com", "http://www.ugc99.com/", "http://try.ugc99.com/", "ug9");
deleteDir("build/www.ugc99.com/ag");
deleteDir("build/www.ugc99.com/ag_try");
deleteDir("build/ug92");
deleteDir("build/ug9");

// E游彩
console.log("E游彩...");
copyDir("build/ag/", "build/ey552/");
copyDir("build/ey55/", "build/ey552/");
copyDir("build/ey552/", "build/www.ey55.com/ey55/");
copyDir("build/ey552/", "build/www.ey55.com/ey55_try/");
copyFullDir("www.ey55.com", "http://www.eycp5.com/", "http://try.eycp5.com/", "ey55");
deleteDir("build/www.ey55.com/ag");
deleteDir("build/www.ey55.com/ag_try");
deleteDir("build/ey552");
deleteDir("build/ey55");
replaceFile(["build/www.ey55.com/ey55/main2.html", "build/www.ey55.com/ey55_try/main2.html"], {
    '<li class="r-wrap r-nowrap1" id="kjzb_div">': '<li class="r-wrap r-nowrap1 hide" id="kjzb_div">',
    '<li class="r-wrap r-nowrap1" id="kjzb2_div">': '<li class="r-wrap r-nowrap1 hide" id="kjzb2_div">',
    '<a id="appdown_link" href="appdownload.html" target="_blank">手机APP下载</a>' : '<a id="appdown_link" href="https://www.eycp55.com" target="_blank">手机APP下载</a>',
    '<li class="r-wrap r-nowrap1 trial-cls link on-off-activity">': '<li class="r-wrap r-nowrap1 trial-cls link hide on-off-activity">',
});
replaceFile(["build/www.ey55.com/agm/index.html", "build/www.ey55.com/agm_try/index.html"], {
    '<li class="app-download"><a href="download.html"> ': '<li class="app-download"><a href="https://www.eycp55.com"> ',
});
replaceFile(["build/www.ey55.com/agm/index.html"], {
    '<li class="pc"><a href="../qgc/index.html"> ': '<li class="pc"><a href="../ey55/index.html"> '
});
replaceFile(["build/www.ey55.com/agm_try/index.html"], {
    '<li class="pc"><a href="../qgc/index.html"> ': '<li class="pc"><a href="../ey55_try/index.html"> '
});

// KK彩票
console.log("KK彩票...");
copyDir("build/ag/", "build/kk/");
copyDir("build/kkcpw1/", "build/kk/");
copyDir("build/kk/", "build/www.kkcpw1.com/kk/");
copyDir("build/kk/", "build/www.kkcpw1.com/kk_try/");
copyFullDir("www.kkcpw1.com", "http://www.kkcpw1.com/", "http://try.kkcpw1.com/", "kk");
deleteDir("build/www.kkcpw1.com/ag");
deleteDir("build/www.kkcpw1.com/ag_try");
deleteDir("build/kk");
deleteDir("build/kkcpw1");

// 多利彩
console.log("多利彩...");
copyDir("build/ag/", "build/dlc552/");
copyDir("build/dlc55/", "build/dlc552/");
copyDir("build/dlc552/", "build/www.dlc55.com/dlc55/");
copyDir("build/dlc552/", "build/www.dlc55.com/dlc55_try/");
copyFullDir("www.dlc55.com", "http://www.dlc55.com/", "http://try.dlc55.com/", "dlc55");
deleteDir("build/www.dlc55.com/ag");
deleteDir("build/www.dlc55.com/ag_try");
deleteDir("build/dlc552");
deleteDir("build/dlc55");
replaceFile(["build/www.dlc55.com/dlc55/main2.html", "build/www.dlc55.com/dlc55_try/main2.html"], {
    '<li class="r-wrap r-nowrap1" id="kjzb_div">': '<li class="r-wrap r-nowrap1 hide" id="kjzb_div">',
    '<li class="r-wrap r-nowrap1" id="kjzb2_div">': '<li class="r-wrap r-nowrap1 hide" id="kjzb2_div">',
    '<li class="r-wrap r-nowrap1 trial-cls link on-off-activity">': '<li class="r-wrap r-nowrap1 trial-cls link hide on-off-activity">',
});

/*
// 菠菜
console.log("* wechat...");
copyFullDir("bocai", "http://www.bocai8877.com/", "http://www.bocai8877.com:9090/");
copyDir("build/wx", "build/bocai/wx");
copyDir("build/group", "build/bocai/group");
replaceFile([ "build/bocai/group/js/admin.min.js",
    "build/bocai/group/js/login.min.js", "build/bocai/wx/js/center.min.js" ], 
{
    "http://www.1hc8.com/" : "http://www.bocai8877.com/",
    "http://www.1hc8.com:9090/" : "http://www.bocai8877.com:9090/",
});
replaceFile([ "build/bocai/admin/login.html", "build/bocai/admin/admin.html" ], 
{
    "// bocai = true;": "bocai = true;",
});
*/
